﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace HangZhouSYB
{
    public class Util
    {
        /// <summary>
        /// 卡号/证号
        /// </summary>
        /// <param name="cardtype">入参:卡类型</param>
        /// <param name="kaxinxi">入参:卡信息</param>
        /// <returns></returns>
        public static string GetCardType(string cardtype, string kaxinxi)
        {
            switch (cardtype)
            {
                case "1": return "K" + kaxinxi;
                case "2": return "Z" + kaxinxi;
                case "3": return "K" + kaxinxi.Substring(0, 32);
                case "4": return "K" + kaxinxi;
                default: return "K" + kaxinxi;
            }
        }

        /// <summary>
        /// 获得结束标志:结束标志[上传条数大于1：第一条标识为1,中间记录统一标识为2,最后一条标识为9 ；上传条数等于1：标识为0]
        /// </summary>
        /// <param name="mingxijls"></param>
        /// <param name="index"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static int GetItemNumber(int mingxijls, int index, int count)
        {
            return mingxijls == 1 ? 0 : (index == 0 ? 1 : (index == count - 1 ? 9 : 2));
        }
        /// <summary>
        /// 获得住院医疗类别
        /// </summary>
        /// <param name="ruyuanlx">入参:入院类别</param>
        /// <param name="shenpibh">入参:单病种审批编号</param>
        /// <param name="yibaobrxx">入参:医保病人信息</param>
        public static string GetYLLB(string ruyuanlx, string shenpibh, string yibaobrxx)
        {
            var str = yibaobrxx.Split('|')[29];
            switch (ruyuanlx)
            {
                case "00": return "21";//普通入院
                case "01": return "21";//急诊入院
                case "02": return "22";//转院入院
                case "03": return shenpibh.StartsWith("8") ? "71" : "72";//单病种入院
                case "04": return "43";//家庭病床入院
                case "05": return (str == "1" || str == "2" || str == "5") ? "24" : "21";//保健干部入院                
                default: return "21";//其它
            }

        }

        public static string getMZJSLB(string YiLiaoLB, string yibaobrxx)
        {
            string fanhui = "";
            var str = yibaobrxx.Split('|')[29];       
            if (str == "1" || str == "2" || str == "5")
            {
                fanhui=(YiLiaoLB=="02"?"19":"17");
            }
            else
            {
                fanhui=(YiLiaoLB=="02"?"3":YiLiaoLB=="06"?"93":"1");
            }
            return fanhui;
        }

        /// <summary>
        /// 转换出院类型
        /// </summary>
        /// <param name="chuyuanlx">入院类型</param>
        /// <param name="shenpibh">审批编号</param>
        public static string GetJSType(string ruyuanlx, string shenpibh)
        {
            switch (ruyuanlx)
            {
                case "00": return "2";//	普通出院 
                case "01": return "2";	//转出医院结算
                case "02": return "2";//	转入医院结算
                case "03": return shenpibh.StartsWith("8") ? "11" : "14";//	单病种入院
                case "04": return "15";//	家庭病房
                case "05": return "18";//	保健干部结算
                default: return "2";//	其它
            }
        }

        public static string ParsetTime(string datetime)
        {
            return string.Format("{0:yyyyMMdd}", string.IsNullOrEmpty(datetime) ? DateTime.Now : DateTime.Parse(datetime));
        }

        /// <summary>
        /// 获得保健干部用药标志
        /// </summary>
        /// <returns></returns>
        public static string GetBJGBYYBZ(string ybbrxx, string xiangmubh)
        {
            string bllx = ybbrxx.Split('|')[28];
            if (bllx == "2" || bllx == "4")
            {
                switch (xiangmubh)
                {
                    case "1000000000000001":
                    case "1000000000000002":
                    case "2000000000000001":
                    case "2000000000000002":
                    case "3000000000000001":
                    case "4000000000000001":
                    case "3000000000000002":
                    case "4000000000000002": return "1";
                    default: return "0";
                }
            }
            return "0";
        }

        /// <summary>
        /// 项目编码|申请数量,审批类别0、1、2传
        /// </summary>
        /// <param name="shenpilb">审批类别</param>
        /// <param name="xmbmornum">项目编号或申请数量</param>
        /// <returns></returns>
        public string GetRegNum(string shenpilb, string xmbmornum)
        {
            switch (shenpilb)
            {
                case "0":
                case "1":
                case "2": return xmbmornum;
                default: return string.Empty;
            }
        }

        /// <summary>
        /// 返回组合后的字符串:分院代码+(登记次数+1)_病人编号
        /// </summary>
        /// <param name="entity">登记信息对象</param>
        /// <param name="count">登记次数</param>
        /// <returns></returns>
        public static string GetZYHM(string fydm, string yyzyhm, decimal count)
        {
            return String.Format("{0}{1:D3}_{2}", fydm, (count + 1).ToString().PadLeft(3, '0'), yyzyhm);
        }

        public static string GetString(string str, int index, int length)
        {
            var list = new List<byte>(Encoding.Default.GetBytes(str));
            if (index + length <= list.Count)
            {
                return Encoding.Default.GetString(list.GetRange(index, length).ToArray()).Trim();
            }
            else
            {
                return Encoding.Default.GetString(list.GetRange(index, list.Count - index).ToArray()).Trim();
            }
        }

        #region 错误提示信息

        /// <summary>
        /// 没有登记状态
        /// SELECT * FROM yb_zydjxx yz WHERE yz.ylbxid='{0}' AND yz.yyzyhm='{1}' AND yz.zydjzt IN (0,1) AND ROWNUM=1 ORDER BY yz.zydjid ASC
        /// </summary>
        public const string MYDJZT = "SELECT * FROM yb_zydjxx yz WHERE yz.ylbxid='{0}' AND yz.yyzyhm='{1}' AND yz.zydjzt IN (0,1) AND ROWNUM=1 ORDER BY yz.zydjid ASC";
        /// <summary>
        /// 获得病人最后的住院登记信息
        /// </summary>
        public const string HDZHZYXX = "SELECT * FROM yb_zydjxx yz WHERE yz.ylbxid='{0}' AND yyzyhm='{1}' AND ROWNUM=1 ORDER BY yz.zydjid ASC";
        /// <summary>
        /// 没有进行登记,请先进行登记
        /// </summary>
        public const string HMDJ = "没有进行登记,请先进行登记";
        /// <summary>
        /// 该病人已经住院登记，不能重复登记
        /// </summary>
        public const string YJDJ = "该病人已经住院登记，不能重复登记";
        /// <summary>
        /// 没有进行住院结算，不能取消
        /// </summary>
        public const string MYJS = "该病人没有进行住院结算或者没有住院登记，不能取消结算";
        /// <summary>
        /// 没有找到病人的结算信息
        /// </summary>
        public const string MYJSXX = "没有找到病人的结算信息";

        #endregion

        /// <summary>
        /// 获取字符串的拼音首字母
        /// </summary>
        /// <param name="CnStr">字符串</param>
        /// <returns>拼音首字母</returns>
        public static string GetStringSpellFirstCode(string CnStr)
        {
            StringBuilder CodeStr = new StringBuilder();
            foreach (char c in CnStr)
            {
                CodeStr.Append(GetCharSpellFirstCode(c));
            }
            return CodeStr.ToString();
        }
        /// <summary>
        /// 获取字符的拼音首字母，如果为英语则直接返回
        /// </summary>
        /// <param name="CnChar"></param>
        /// <returns></returns>
        public static char GetCharSpellFirstCode(char CnChar)
        {
            long iCnChar;
            byte[] ZW = System.Text.Encoding.Default.GetBytes(CnChar.ToString());
            //如果是中文则直接返回
            if (ZW.Length == 1)
            {
                return CnChar;
            }
            else
            {
                int i1 = (short)(ZW[0]);
                int i2 = (short)(ZW[1]);
                iCnChar = i1 * 256 + i2;
            }
            if ((iCnChar >= 45217) && (iCnChar <= 45252))
            {
                return 'a';
            }
            else if ((iCnChar >= 45253) && (iCnChar <= 45760))
            {
                return 'b';
            }
            else if ((iCnChar >= 45761) && (iCnChar <= 46317))
            {
                return 'c';
            }
            else if ((iCnChar >= 46318) && (iCnChar <= 46825))
            {
                return 'd';
            }
            else if ((iCnChar >= 46826) && (iCnChar <= 47009))
            {
                return 'e';
            }
            else if ((iCnChar >= 47010) && (iCnChar <= 47296))
            {
                return 'f';
            }
            else if ((iCnChar >= 47297) && (iCnChar <= 47613))
            {
                return 'g';
            }
            else if ((iCnChar >= 47614) && (iCnChar <= 48118))
            {
                return 'h';
            }
            else if ((iCnChar >= 48119) && (iCnChar <= 49061))
            {
                return 'j';
            }
            else if ((iCnChar >= 49062) && (iCnChar <= 49323))
            {
                return 'k';
            }
            else if ((iCnChar >= 49324) && (iCnChar <= 49895))
            {
                return 'l';
            }
            else if ((iCnChar >= 49896) && (iCnChar <= 50370))
            {
                return 'm';
            }

            else if ((iCnChar >= 50371) && (iCnChar <= 50613))
            {
                return 'n';
            }
            else if ((iCnChar >= 50614) && (iCnChar <= 50621))
            {
                return 'o';
            }
            else if ((iCnChar >= 50622) && (iCnChar <= 50905))
            {
                return 'p';
            }
            else if ((iCnChar >= 50906) && (iCnChar <= 51386))
            {
                return 'q';
            }
            else if ((iCnChar >= 51387) && (iCnChar <= 51445))
            {
                return 'r';
            }
            else if ((iCnChar >= 51446) && (iCnChar <= 52217))
            {
                return 's';
            }
            else if ((iCnChar >= 52218) && (iCnChar <= 52697))
            {
                return 't';
            }
            else if ((iCnChar >= 52698) && (iCnChar <= 52979))
            {
                return 'w';
            }
            else if ((iCnChar >= 52980) && (iCnChar <= 53640))
            {
                return 'x';
            }
            else if ((iCnChar >= 53689) && (iCnChar <= 54480))
            {
                return 'y';
            }
            else if ((iCnChar >= 54481) && (iCnChar <= 55289))
            {
                return 'z';
            }
            else return ('\0');
        }

        public static void ConvertDM(dynamic item, string ybbrxx)
        {
            string bllx = ybbrxx.Split('|')[28];
            if (item.FEIYONGLX == "0" && item.YIBAOZFBL == "1")
            {
                switch (item.XIANGMUXH as string)
                {
                    case "9000000000000004":
                    case "9000000000000005":
                        if (bllx != "1" && bllx != "3")
                        {
                            item.YIBAODM = "7000000000000001";
                            item.ZILIJE = "0";
                            item.ZIFEIJE = item.JINE;
                        } break;
                    case "9000000000000006":
                        switch (bllx)
                        {
                            case "1":
                            case "2":
                            case "3":
                            case "4":
                            case "17":
                            case "18": break;
                            default: item.YIBAODM = "7000000000000001";
                                item.ZILIJE = "0";
                                item.ZIFEIJE = item.JINE;
                                break;
                        } break;
                    default: break;
                }
            }
            if (item.FEIYONGLX == "3")
            {
                if (item.ZILIJE == "0" && item.ZIFEIJE == "0")
                {
                    item.YIBAODM = "5000000000000001";
                    item.YIBAOZFBL = "1";
                    item.YIBAODJ = "1";
                }
            }
            switch (bllx)
            {
                case "2":
                case "4":
                    if (item.XIANGMUXH == "3000000000000001" || item.XIANGMUXH == "4000000000000001")
                    {
                        item.YIBAODJ = "3";
                    } break;
                default: break;
            }
        }

        public static string GetTSDYLB(string[] output)
        {
            return Convert.ToInt32(output[29]) > 0 ? output[29] == "0" ? output[29] : (Convert.ToInt32(output[29]) + 1).ToString() : output[49];
        }

        public static T ObjectCopy<T>(T obj)
        {
            object retval;
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                //序列化成流
                bf.Serialize(ms, obj);
                ms.Seek(0, SeekOrigin.Begin);
                //反序列化成对象
                retval = bf.Deserialize(ms);
                ms.Close();
            }
            return (T)retval;
        }
    }
}
